package org.vectrics.mail;

import org.vectrics.SystemException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.LocalMachineUtils;
import org.vectrics.company.Company;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;

public class VectricsEmailTemplate extends EmailTemplate {
	private Cell contentCell = null;
	
	public VectricsEmailTemplate() {
	}
	
	protected String getWidth() {
		return("100%");
	}
	
	protected void doRender() {
		Domain domain = CoreServiceLocator.getSystemService().findDomain(this.getDomain().getId());
		this.getHead().addElement("<style><!-- " +
			    ".bannerBg{BACKGROUND: #000055} " +
				".bannerFont{FONT-SIZE: 16px; COLOR: #EEEEEE; FONT-FAMILY: verdana, arial, sans-serif; line-height: 24px} " +
				".footerFont{FONT-SIZE: 9px; COLOR: #ffffff; FONT-FAMILY: verdana, arial, sans-serif; line-height: 10px} " +
				".bgColor{BACKGROUND: #ffffff} " +
				"--></style>");
		this.getBody().setClass("bgColor");
		Table table = new Table();
		table.setWidth(this.getWidth());
		this.getBody().addElement(table);
		Row bannerRow = table.addRow();

		Cell bannerCell = bannerRow.addCell();
		bannerCell.setClass("bannerBg");
		renderBanner(bannerCell);
		
		Row contentRow = table.addRow();
		
		contentCell = contentRow.addCell();
		Row spaceRow = table.addRow();
		Cell spaceCell = spaceRow.addCell();
		spaceCell.addText("stdFont", "&nbsp;");

		Row footerRow = table.addRow();
		Cell footerCell = footerRow.addCell();
		footerCell.setBgColor("#777777");
		String hostName = LocalMachineUtils.findHostName()
		   		+ "  /  " + LocalMachineUtils.findHostAddress();
		footerCell.setAlign("center");
		footerCell.addText("footerFont", "Host: " + hostName + "<br>" 
				+ "Domain: " + domain.getDescription());
	}

	
	protected Cell getContentCell() {
		return this.contentCell;
	}
	
	protected void renderBanner(Cell bannerCell) {
		//bannerCell.setBgColor("#002255");
		log.debug("Reload domain - in case it was loaded by other hibernate session");
		Domain domain = CoreServiceLocator.getSystemService().findDomain(this.getDomain().getId());
		if (domain == null)
			throw new SystemException("Could not find domain");
		
		Company company = domain.getPrimaryCompany();
		String companyName = company.getName();
		bannerCell.addText("bannerFont", companyName);
	}

}
